package com.example.examination;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class MainActivity2 extends AppCompatActivity{
    private ListView tv_lv2;
    private TextView tv_name;
    private TextView tv_leixing;
    private TextView tv_xingzi;
    private TextView tv_lx;
    private int i=0;
    private TextView tv_paixu;
    private List<MainActivity_list2> mainActivity_list2s = new ArrayList<>();
    private Button bu_syt;
    private MeAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        initdata();
    }

    private void initView() {
        tv_lv2 = (ListView) findViewById(R.id.tv_lv2);
        tv_paixu=(TextView)findViewById(R.id.tv_paixu);
        bu_syt = (Button) findViewById(R.id.bu_syt);
        tv_lx=(TextView)findViewById(R.id.tv_lx);
        tv_paixu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (i==0){
                    i=1;
                    tv_paixu.setText("薪资⬆");
                    Collections.sort(mainActivity_list2s.get(0).getData(),new order());
                }else {
                    i=0;
                    tv_paixu.setText("薪资⬇");
                    Collections.sort(mainActivity_list2s.get(0).getData(),new order1());
                }
                adapter.notifyDataSetChanged();
            }
        });
        tv_lx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_lx.setText("类型√");
                tv_paixu.setText("薪资");
                Collections.sort(mainActivity_list2s.get(0).getData(),new order2());
                adapter.notifyDataSetChanged();
            }
        });
        bu_syt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity2.this,MainActivity.class));
            }
        });
    }
    public interface BlogService {
        @GET("getEmpMarketList")
        Call<ResponseBody> getBlog();
    }

    private void initdata() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogService service = retrofit.create(BlogService.class);
        Call<ResponseBody> call = service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson = new Gson();
                try {
                    MainActivity_list2 carlist = gson.fromJson(response.body().string(), MainActivity_list2.class);
                    Log.i("TAG", "bbbbb" + response.body().string());
                    mainActivity_list2s.add(carlist);
                    Collections.sort(mainActivity_list2s.get(0).getData(), new order2());
                    adapter = new MeAdapter();
                    tv_lv2.setAdapter(adapter);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    class MeAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mainActivity_list2s.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1 = (View) View.inflate(MainActivity2.this, R.layout.activity_main_2, null);
            tv_name = (TextView) view1.findViewById(R.id.tv_name);
            tv_leixing = (TextView) view1.findViewById(R.id.tv_leixing);
            tv_xingzi = (TextView) view1.findViewById(R.id.tv_xingzi);
            tv_name.setText(mainActivity_list2s.get(0).getData().get(i).getName());
            tv_leixing.setText(mainActivity_list2s.get(0).getData().get(i).getCls());
            tv_xingzi.setText(mainActivity_list2s.get(0).getData().get(i).getSalary() + "");
            Log.i("TAG", "aaaaa" + mainActivity_list2s.get(0).getData().get(i).getSalary());
            return view1;
        }
    }

    private class order implements Comparator<MainActivity_list2.DataBean> {
        @Override
        public int compare(MainActivity_list2.DataBean m0, MainActivity_list2.DataBean m1) {
            return m0.getSalary() - m1.getSalary();
        }
    }
    private class order1 implements Comparator<MainActivity_list2.DataBean> {
        @Override
        public int compare(MainActivity_list2.DataBean m0, MainActivity_list2.DataBean m1) {
            return m1.getSalary() - m0.getSalary();
        }
    }
    private class order2 implements Comparator<MainActivity_list2.DataBean> {
        @Override
        public int compare(MainActivity_list2.DataBean m0, MainActivity_list2.DataBean m1) {
            return m0.getCls().compareTo(m1.getCls());
        }
    }
}
